"use strict";

var Router = require('koa-router');
var config = require('../../../config/config.js');
var daoDispatcher = require('../../../dao/category.js');
var debug = require('debug')('category');

console.log('router:' + config.baseUrl + '/v2/category');
var router = new Router({
    prefix: config.baseUrl + '/v2/category'
});

router.get('/test', function*() {
    this.body = 'test work fine';
});

/**
 * get /api/v2/category?id=xxx
 */
router.get('/', function*() {
    var result = yield daoDispatcher.findById(this.query.id);
    this.body = result;
});

/**
 * post /api/v2/category
 {
   "name":"world"
 }
 */
router.post('/', function*() {
    var result = yield daoDispatcher.addOne(this.request.inParams);
    this.body = result;
});

/**
 * post /api/v2/category/batch
 [{
	"name":"护肤",
	"sub":[
        {"name":"脸部护肤","sub":[{"name":"面霜"}]},
        {"name":"手部护肤","sub":[{"name":"手霜"}]}
	]
},{
	"name":"整形",
	"sub":[
        {"name":"胸部整形","sub":[{"name":"胸变大"}]}
    ]
}]
 */
router.post('/batch', function*() {
    var result = yield daoDispatcher.addBatch(this.request.inParams);
    this.body = result;
});


/**
 * get /api/v2/category/exists?cates=美发,吃饭,护肤
 */
router.get('/exists', function*() {
    let cates = this.query.cates.split(',');
    var result = yield daoDispatcher.exists(cates);
    this.body = result;
});
module.exports = router.routes();